package com.zscat.mallplus.qsg.contrller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zscat.mallplus.annotation.SysLog;
import com.zscat.mallplus.qsg.QsgDevice;
import com.zscat.mallplus.qsg.QsgMeal;
import com.zscat.mallplus.qsg.QsgRechargeOrder;
import com.zscat.mallplus.qsg.service.IDeviceService;
import com.zscat.mallplus.qsg.service.IMealService;
import com.zscat.mallplus.qsg.service.IQsgRechargeOrderService;
import com.zscat.mallplus.ums.entity.UmsMember;
import com.zscat.mallplus.ums.service.IUmsMemberService;
import com.zscat.mallplus.util.DateUtil;
import com.zscat.mallplus.util.UserUtils;
import com.zscat.mallplus.utils.CommonResult;
import com.zscat.mallplus.utils.ValidatorUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Slf4j
@RestController
@Api(tags = "RechargeOrderController", description = "充值订单管理")
@RequestMapping("/qsg/rechargeOrder")
public class RechargeOrderController {

    @Resource
    private IQsgRechargeOrderService rechargeOrderService;
    @Autowired
    private IUmsMemberService memberService;

    @SysLog(MODULE = "qsg", REMARK = "根据条件查询所有充值订单列表")
    @ApiOperation("根据条件查询所有充值订单列表")
    @GetMapping(value = "/list")
    public Object getRechargeOrderByPage(@RequestParam(required = false) String nickname,
                                  @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                  @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize
    ) {
        QueryWrapper qw = new QueryWrapper();
        QueryWrapper memberQw = new QueryWrapper();
        List<Long> ids = new ArrayList<>();
        memberQw.like("nickname",nickname);
        qw.orderByDesc("create_datetime");
        qw.eq("store_id", UserUtils.getCurrentMember().getQsgStoreId());
        qw.eq("order_status",2);
        List<UmsMember> members = memberService.list(memberQw);
        if (StringUtils.isNotBlank(nickname)){
            qw.eq("meal_name",nickname);
        }
        if (null!=members&&members.size()>0){
            for (UmsMember member : members){
                ids.add(member.getId());
            }
            qw.in("member_id",ids);
        }
        try {
            IPage<QsgRechargeOrder> orderIPage = rechargeOrderService.page(new Page<>(pageNum,pageSize),qw);
            for (QsgRechargeOrder order : orderIPage.getRecords()){
                UmsMember member = memberService.getById(order.getMemberId());
                if (null==member){
                    continue;
                }
                order.setMemberNickname(member.getNickname());
            }
            return new CommonResult().success(orderIPage);
        } catch (Exception e) {
            log.error("根据条件查询所有充值订单列表：%s", e.getMessage(), e);
        }
        return new CommonResult().failed();
    }
}
